/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */


package com.smg.community.controller;

import java.io.IOException;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseRestSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.org.rapid_framework.page.Page;
import com.google.gson.Gson;
import com.smg.community.model.CrmRole;
import com.smg.community.model.CrmRoleRight;
import com.smg.community.model.CrmUser;
import com.smg.community.vo.query.CrmRoleQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/crmrole")
public class CrmRoleController extends BaseRestSpringController<CrmRole,java.lang.Integer>{
	
	/** 角色页面
	 *  */
	@RequestMapping("/show")
	public String show(ModelMap model,CrmRoleQuery query,HttpServletRequest request,HttpServletResponse response) throws IOException {
		model.addAttribute("rightList",crmRightManager.getAll());
		return "/sysmgr/role";
	}
	/** 角色列表 
	 * @throws IOException */
	@RequestMapping("/ajax/list")
	public void index(ModelMap model,CrmRoleQuery query,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		Writer out = response.getWriter();
		response.setContentType("text/plain");
		Map<String,String> resultMap = new HashMap<String,String>();
		Gson gson = new Gson();
		setPagerModel(query,request);
		
		String searchFlag = request.getParameter("searchFlag");
		if(null != searchFlag && !"".equals(searchFlag)){
			query.setFlag(Integer.parseInt(searchFlag));
		}
		query.setName(request.getParameter("searchName"));
		@SuppressWarnings("unchecked")
		Page<CrmUser> page = crmRoleManager.findPage(query);
		
		StringBuffer jsonbf = new StringBuffer();
		jsonbf.append("{start:'" + query.getPageNumber() + "',limit:'" + query.getPageSize() + "',count:'" + page.getTotalCount() + "',root:");
		out.write(jsonbf.append(gson.toJson(page.getResult())).toString()+"}");
		out.close();
	}
	//新增或修改用户
	@RequestMapping(value = "/ajax/add")
	public void editUserInfo(ModelMap model,CrmRole entity,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		Writer out = response.getWriter();
		CrmUser crmUser = getLoginUser(request);
		//entity.setCityName(value);
		entity.setName(URLDecoder.decode(entity.getName(),"UTF-8"));
		entity.setRoleDesc(URLDecoder.decode(entity.getRoleDesc(),"UTF-8"));
		entity.setFlag(entity.getFlag());
		if(null == entity.getId() || entity.getId() <= 0){
			CrmRoleQuery query = new CrmRoleQuery();
			query.setName(entity.getName());
			if(null != crmRoleManager.getRoleByRoleQuery(query)){
				writeMessage(out, 0);
				return;
			}
			crmRoleManager.save(entity);
		}else{
			crmRoleManager.update(entity);
		}
		writeMessage(out, 1);
	}
	/** 获取角色对应权限
	 * @throws IOException */
	@RequestMapping(value = "/ajax/getByRoleId",method=RequestMethod.POST)
	public void getByRoleId(ModelMap model, String roleId,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		Writer out = response.getWriter();
		response.setContentType("text/plain");
		Gson gson = new Gson();
		List<CrmRoleRight> list = crmRoleRightManager.getByRoleId(roleId);
		out.write("{\"options\":" + gson.toJson(list) + "}");
		out.close();
	}
	/** 更新用户权限
	 * @throws IOException */
	@RequestMapping(value = "/ajax/updateUserRight",method=RequestMethod.POST)
	public void updateUserRight(ModelMap model, String roleId,String rights,HttpServletRequest request,HttpServletResponse response) throws IOException {
		Writer out = response.getWriter();
		response.setContentType("text/plain");
		CrmUser crmUser = getLoginUser(request);
		if(null != roleId && !"".equals(roleId)){
			crmRoleRightManager.deleteByRoleId(roleId);
			if(null != rights && !"".equals(rights)){
				Map<String, String> parentCode = new HashMap<String, String>();
				for(String right : rights.split(",")){
					parentCode.put(right.substring(0,3), right.substring(0,3));
					CrmRoleRight entity = new CrmRoleRight();
					entity.setRoleId(Integer.parseInt(roleId));
					entity.setRightCode(right);
					crmRoleRightManager.save(entity);
				}
				for(String key : parentCode.keySet()){
					CrmRoleRight entity = new CrmRoleRight();
					entity.setRoleId(Integer.parseInt(roleId));
					entity.setRightCode(parentCode.get(key));
					crmRoleRightManager.save(entity);
				}
			}
			writeMessage(out, 1);
		}else{
			writeMessage(out, 0);
		}
	}
	
}

